﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 获取任务执行所需要填写的参数列表
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("ETL", "GetTaskParameterList", "1.0")]
    public class GetTaskParameterListAction : BaseAction<GetTaskParameterListParams, GetTaskParameterListResult>
    {
        protected override ActionResult<GetTaskParameterListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            GetTaskParameterListParams opParams)
        {
            SER.TaskService service = new SER.TaskService();
            ListResult<VIW.VTaskParameter> result = service.GetTaskParameterList(opParams.TaskId);
            if(result.IsSuccess)
            {
                GetTaskParameterListResult actResult = new GetTaskParameterListResult();
                foreach (var vItem in result.Data)
                {
                    GetTaskParameterListResult.Item item = new GetTaskParameterListResult.Item();
                    item.Name = vItem.Name;
                    item.Type = vItem.DataType;

                    if(vItem.ValueType == CPPEI.ETL.Domain.DEF.TaskParameterType.Value)
                    {
                        item.Value = vItem.ValueString;
                    }
                    else
                    {
                        item.Value = string.Empty;
                    }
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetTaskParameterListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetTaskParameterListResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class GetTaskParameterListParams
    {
        [Parameter]
        public Guid TaskId { get; set; }
    }

    public class GetTaskParameterListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string Name { get; set; }

            public string Type { get; set; }

            public string Value { get; set; }
        }
    }
}
